package com.example.login;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.login.db.UserDbHelper;
import com.example.login.entity.UserInfo;

public class RegisterActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;

    private SharedPreferences msharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);

        // 获取msharedPreferences
        msharedPreferences = getSharedPreferences("user", MODE_PRIVATE);

        // 初始化控件
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);

        // 返回登录页面事件
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 销毁页面即可
                finish();
            }
        });

        // 点击注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString();
                String psw = password.getText().toString();

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(psw)) {
                    Toast.makeText(RegisterActivity.this, "请输入用户名密码", Toast.LENGTH_SHORT).show();
                } else {
                    //SharedPreferences.Editor edit = msharedPreferences.edit();
//                    edit.putString("name", name);
//                    edit.putString("psw", psw);
//                    edit.commit();

                    int row = UserDbHelper.getInstance(RegisterActivity.this).register(name, psw, 1);
                    if (row > 0) {
                        Toast.makeText(RegisterActivity.this, "注册成功,请登录", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                }
            }
        });
    }
}